home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11613 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: solon.com!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: const pointer confusion...
  5. Date: 25 Mar 1996 06:24:21 -0600
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4j639l$3ju@solutions.solon.com>
  10. References: <4j06gm$7oa@solutions.solon.com> <4j41hs$nku@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <4j41hs$nku@solutions.solon.com>,
  14. The Amorphous Mass  <robinson@blue.weeg.uiowa.edu> wrote:
  15. >> 2) int *const p;
  16. >>     p is a pointer to an integer.  *p can be assigned to, but p can
  17. >>     never be made to point to another address in memory, right?
  18. >
  19. >  This is a syntax error.
  20.  
  21. No it is not. He is right in his summary of the semantics, but not in the name:
  22. p is a constant pointer to an integer.
  23.  
  24. >> 3) int const *p;
  25. >>     What the heck is this?  I can't find anything like this in my 
  26. >>     books, but my compiler thinks everything is hunky doory!???
  27. >
  28. >  This is a constant pointer to an integer.  *p can be changed, but p 
  29. >cannot.
  30.  
  31. Wrong again: ``int const'' is a permutation of ``const int'', a syntactic
  32. variant of the same semantic declaration specifier list. Both refer to a
  33. constant integer. The ``*p'' is a declarator which makes p a pointer to such a
  34. constant integer.
  35.  
  36. Look at the grammar productions for the syntactic unit
  37. ``declaration-specifiers'', K&R2 p. 210. It clearly shows that storage class
  38. specifiers, type qualifiers and type specifiers may appear in any order. There
  39. are some semantic restrictions on this syntactic freedom: a storage class
  40. specifier may appear only once, for instance, as may a type specifier:
  41.  
  42.     int typedef extern auto foo;
  43.  
  44. is syntactically valid, but semantically incorrect, because it has three
  45. storage specifiers.
  46.  
  47. Pump that through GCC, for instance, and you don't get a syntax error, but a
  48. semantic error warning you that you have more than one datatype in the
  49. declaration, as well as more than one storage class. How the specifiers are
  50. ordered doesn't matter: ``int typedef x;'' is just as valid as
  51. ``typedef int x;''
  52.  
  53. >  Also, there's
  54. >
  55. >  4) const int const *p;
  56. >
  57. >  Which is a constant pointer to a constant integer; neither *p nor 
  58. >p can change.
  59.  
  60. Wrong. ``const int const'' is just a redundancy. It is the same as ``const
  61. int'' or ``int const''. A well-written compiler should warn you that you have
  62. the same type qualifier appearing twice.
  63.  
  64. In fact I just tried it with GCC and, indeed!, it warns about ``duplicate
  65. const'' in the declaration! Hooray!
  66.  
  67.  
  68. I think you were trying to say:
  69.  
  70.     4) const int * const p;
  71. -- 
  72.